<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 外卖点心管理表格
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button>
                <el-input v-model="query.name" placeholder="用户名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button
                    type="this.level== 1? 'success' : 'info' "
                    icon="el-icon-s-tools"
                    class="handle-add mr10"
                    @click="identitychange()"
                > <span v-if="this.level== 1 ">我是老板点击切换农民工</span> 
                    <span v-else>我是农民工点击升级老板</span> </el-button>
                <el-button
                    type="primary"
                    icon="el-icon-folder-add"
                    class="handle-add mr10"
                    @click="handleAdd()"
                >添加菜品</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="菜品ID" width="70" align="center"></el-table-column>
                <el-table-column prop="productName" label="菜品名称" align="center"></el-table-column>
              
                <el-table-column label="图片(查看大图)" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.productPic"
                            :preview-src-list="[scope.row.productPic]"
                        ></el-image>
                    </template>
                </el-table-column>
                 <el-table-column label="推荐价格(元)" align="center">
                    <template slot-scope="scope">￥{{scope.row.productPrice}}</template>
                </el-table-column>
                <el-table-column prop="productNum" label="推荐库存" align="center"></el-table-column>
                <!-- <el-table-column prop="address" label="推荐起购数量" align="center"></el-table-column>
                <el-table-column prop="address" label="推荐限购数量" align="center"></el-table-column> -->
                <el-table-column prop="type" label="类别" align="center">点心</el-table-column>
                <!-- <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.state==='成功'?'success':(scope.row.state==='失败'?'danger':'')"
                        >{{scope.row.state}}</el-tag>
                    </template>
                </el-table-column> -->

                <!-- <el-table-column prop="date" label="注册时间"></el-table-column> -->
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.row.id, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="菜品名称">
                    <el-input v-model="form.productName"></el-input>
                </el-form-item>
                <el-form-item label="推荐价格">
                    <el-input v-model="form.productPrice"></el-input>
                </el-form-item>
                 <el-form-item label="推荐库存">
                    <el-input v-model="form.productNum"></el-input>
                </el-form-item>
                <el-form-item  label="换管理者">
                     <el-select v-model="levelchanged" placeholder="请选择">
                        <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增弹框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="30%">
            <el-form  ref="addForm" :model="addForm" label-width="70px">
                <el-form-item label="菜品名称">
                    <el-input v-model="addForm.productName"></el-input>
                </el-form-item>
                <el-form-item label="上传图片">
                    <input type="file" @change="onchange($event)" />
                </el-form-item>
                <el-form-item label="推荐价格">\
                    <el-input v-model="addForm.productPrice" readonly><i slot="suffix" class="el-input__icon el-icon-caret-top" @click='addPrice()'></i><i slot="suffix" class="el-input__icon el-icon-caret-bottom" @click='lowPrice()'></i></el-input>
                </el-form-item>
                <el-form-item label="推荐库存">
                    <el-input v-model="addForm.productNum" readonly><i slot="suffix" class="el-input__icon el-icon-caret-top" @click='addStock()'></i><i slot="suffix" class="el-input__icon el-icon-caret-bottom" @click='lowStock()'></i></el-input>
                </el-form-item>
                <el-form-item  label="选管理者">
                     <el-select v-model="leveladd" placeholder="请选择">
                        <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import qs from 'qs'
import { fetchData,getOrderData,deleteOrderData,deleteOrderDataAll,updateProduct,putProduct,getImgUrl,getBossProduct} from '../../api/index';

export default {
    name: 'basetable',
    data() {
        return {
            leveladd:'',
            levelchanged:'',
            options:[{
                    label:'员工',
                    value:0
                },
                {
                    label:'老板',
                    value:1
                }]
            ,
            level:'',
            img:'',
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 10
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            addVisible:false,
            pageTotal: 0,
            form: {},
            addForm:{
               productPrice:0,
               productNum:0,
               productPic:'',
               level:0
            },
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        onchange(e){
            console.log(e);
            var formData=new FormData();
            formData.append('file',e.target.files[0]);
            // console.log(formData);
            getImgUrl(formData).then(res =>{
                console.log(res);
                this.img = res.data.url
            })
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            //获取外卖订单数据
            getOrderData().then(res=>{
                this.level=res.data.level
                this.tableData=res.data.list;
                this.pageTotal=res.data.list.length;
            })
        },
        
       
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    // console.log(index);
                    // this.$message.success('删除成功');
                    // this.tableData.splice(index, 1);
                    deleteOrderData({id:index}).then(res =>{
                        this.$message.success('删除成功');
                        this.getData()
                    })
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
            // console.log(val);
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let ids = new Array;
            // this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
               ids.push(this.multipleSelection[i].id)
            }
            console.log(ids,typeof(ids));
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    // console.log(index);
                    // this.$message.success('删除成功');
                    // this.tableData.splice(index, 1);qs.stringify(ids,{ indices: false })
                    deleteOrderDataAll(ids).then(res =>{
                        this.$message.success('删除成功');
                        this.getData()
                    })
                    
                })
                .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            // console.log(row)
            this.idx = index;
            this.form = row;
            console.log(this.form);
            this.editVisible = true;
        },

        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            console.log(this.levelchanged);
            this.form.level =this.levelchanged
            console.log(this.form);
            // this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            updateProduct(this.form).then(res =>{
                this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                this.getData()
            })
           
        },
        //添加菜品
        handleAdd(){
            // this.addForm={}
            this.addForm={
               productPrice:0,
               productNum:0,
               productPic:''
            }
            this.addVisible=true;
        },
        //添加价格
        addPrice(){
            this.addForm.productPrice++;
        },
        //降低价格
        lowPrice(){
            if(this.addForm.productPrice>0){
                this.addForm.productPrice--;
            }
            else{
                this.$message.warning('价格已经为0，没法再降价了！');
            }
        },
        //添加库存
        addStock(){
            this.addForm.productNum++;
        },
        //降低库存
        lowStock(){
            if(this.addForm.productNum>0){
                this.addForm.productNum--;
            }
            else{
                this.$message.warning('库存已经为0，没法再减少了！');
            }
        },
        // //添加起购数量
        // addStart(){
        //     this.addForm.RecommendedStart++;
        // },
        // //降低起购数量
        // lowStart(){
        //     if(this.addForm.RecommendedStart>0){
        //         this.addForm.RecommendedStart--;
        //     }
        //     else{
        //         this.$message.warning('起购数量已经为0，没法再减少了！');
        //     }
        // },
        // //添加限购数量
        // addLimit(){
        //     this.addForm.RecommendedLimit++;
        // },
        // //降低限购数量
        // lowLimit(){
        //     if(this.addForm.RecommendedLimit>0){
        //         this.addForm.RecommendedLimit--;
        //     }
        //     else{
        //         this.$message.warning('限购数量已经为0，没法再减少了！');
        //     }
        // },
        // 保存添加外卖点心
        saveAdd() {
            this.addVisible = false;
            this.addForm.productPic=this.img
            console.log(this.leveladd);
            console.log(this.addForm);
            this.addForm.level=this.leveladd
            putProduct(this.addForm).then(res =>{
                this.$message.success(`添加成功`);
                this.getData()

            })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        identitychange(){
            if(this.level==0){
                getBossProduct().then(res=>{
                console.log(res);
                this.level=res.data.level
                this.tableData=res.data.list;
                this.pageTotal=res.data.list.length;
            })
            }
            else{
                this.getData()
            }
            
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.handle-add{
    float:right
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
